/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package carcassonne.initialize;

import carcassonne.gameplay.GameTile;
import carcassonne.gameplay.GameTileType;
import carcassonne.gameplay.NextAvailablePositions;
import carcassonne.gameplay.OccupiedPositions;
import carcassonne.gameplay.RemainingGameTiles;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author tylerb
 */
public class GameInit {

    private final String curdir = "src\\carcassonne\\initialize\\";
    private final String gameTileTypeFile = curdir + "gameTileType.xml";
    private final String gameTileListFile = curdir + "gameTileList.xml";
    Document dom;
    private List gameTileTypeList = new ArrayList();
    private List gameTileList = new ArrayList();
    
    public void initializeGame() {

        //Parse the Game Tile Type XML file
        parseXmlFile(gameTileTypeFile);
        parseDocument(this.getGameTileTypeList(), "gameTileType", new GameTileType());
        
        //Parse the Game Tile List XML file
        parseXmlFile(gameTileListFile);
        parseDocument(this.getGameTileList(), "gameTile", new GameTile());
        
        //Initialize the lists
        RemainingGameTiles.initialize(this.getGameTileList());
        OccupiedPositions.initialize(100); //This should be in a properties file
        NextAvailablePositions.initialize();
        
                
    }

    private void parseDocument(List myList, String tagName, Object myObject) {

        //get the root element
        Element docEle = dom.getDocumentElement();

        //get a nodelist of  elements
        NodeList nl = docEle.getElementsByTagName(tagName);
        if (nl != null && nl.getLength() > 0) {
            for (int i = 0; i < nl.getLength(); i++) {

                //get the employee element
                Element el = (Element) nl.item(i);

                //get the Employee object
                if (myObject instanceof GameTileType) {
                    GameTileType gtt = new GameTileType(el);
                    myList.add(gtt);
                } else if (myObject instanceof GameTile) {
                    GameTile gt = new GameTile(el);
                    myList.add(gt);
                }
                

                //add it to list
                
            }
        }
    }

    private void parseXmlFile(String file) {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        
        try {
            //Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            //parse using builder to get DOM representation of the XML file
            dom = db.parse(file);
        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    public List getGameTileTypeList() {
        return gameTileTypeList;
    }
    
    public List getGameTileList() {
        return gameTileList;
    }
    
}
